import math

import pandas as pd
import numpy as np

df_trade = pd.read_csv('tianchi_mum_baby_trade_history.csv')

df_baby = pd.read_csv('tianchi_mum_baby.csv')


gender_map = {}  # key value

buy_count = {}
for index, row in df_trade.iterrows():
    if row.user_id not in buy_count:
        buy_count[row.user_id] = row.buy_mount
    else:
        buy_count[row.user_id] += row.buy_mount

for index, row in df_baby.iterrows():
    gender_map[row.user_id] = (row.gender, buy_count[row.user_id])

total_boy_buy_mount = 0  # 男婴的购买总量
total_boy_count = 0  # 男婴的数量

total_girl_buy_mount = 0
total_girl_count = 0

for item in gender_map.values():
    if item[0] == 0:
        total_boy_count += 1
        total_boy_buy_mount += item[1]
    elif item[0] == 1:
        total_girl_count += 1
        total_girl_buy_mount += item[1]
print('男婴的平均购买力', total_boy_buy_mount / total_boy_count)
print('女婴的平均购买力', total_girl_buy_mount / total_girl_count)
print('购买力对比 %f:1' % (total_boy_buy_mount / total_boy_count / (total_girl_buy_mount / total_girl_count)))

# 统计每年的购买数量， 并打印出来
'''
1. 遍历交易信息表的每一行
2. 从日期的数据提取出 订单交易年份
3. 统计出每一年的购买总量
'''

year_dic = {}  # key：年份  value:当年的销售数量  参考code.py文件中的 pinshu_c的写法。
#  int(20141231 / 10000)
for index, row in df_trade.iterrows():
    y = int(row.day / 10000)
    if y not in year_dic:
        year_dic[y] = 0
    year_dic[y] += row.buy_mount
print(year_dic)

'''
1. 遍历交易信息表的每一行
2. 从日期的数据提取出 订单交易季度
3. 统计出每一季度的购买总量 20141231 后边不要用除法， 前边不要用取余  %
4. 用季度来保存结果

'''

season_dict = {}
for index, row in df_trade.iterrows():
    m = int(row.day % 10000 / 100)
    season = math.ceil(m / 3)
    if season not in season_dict:
        season_dict[season] = 0
    season_dict[season] += row.buy_mount
print(season_dict)

'''
分别统计每一个一级类目的总销量，并保存字典里面
'''
category1_dict = {}

for index, row in df_trade.iterrows():
    cat_id = row.category_1
    if cat_id not in category1_dict:
        category1_dict[cat_id] = 0
    category1_dict[cat_id] += row.buy_mount

print(category1_dict)